/**
 * @FileName: Permission.java
 * @Author
 * @Description:
 * @Date 2015年12月23日 下午4:01:50
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.model;

import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Model;

@TableBind(tableName = "sys_permission")
public class Permission extends Model<Permission>{
    private static final long      serialVersionUID = 8934654052069159735L;
    public static final Permission dao              = new Permission();

    public List<Permission> getPermission(int roleId){
        SysRole role = SysRole.dao.findById(roleId);
        int type = role.getInt("type");
        String sql = "";
        // if(type == 2){
        // /* 超级管理员，获取所有权限 */
        // sql = " select id, name, parent_id, sort, type, level from
        // sys_permission where is_deleted=0 order by level asc, sort asc";
        // List<Permission> perms = dao.find(sql);
        // return perms;
        // } else{
            /* 获取所拥有的权限 */
            sql = " select id, name, parent_id, sort, type, level from sys_permission where is_deleted=0 and "
                    + " id in (select permission_id from sys_role_permission where is_deleted=0 and role_id=?) order by level asc, sort asc";
            List<Permission> perms = dao.find(sql, roleId);
            return perms;
        // }

    }

    public List<Permission> getAllPermission(){
        return dao.find("select * from sys_permission where is_deleted=0");
    }

    public String getSelect(){
        String select = " select p.*, (select user_name from sys_user u where p.created_by = u.id) as creator, "
                + " (select user_name from sys_user u where p.last_updated_by = u.id) as updator ";


        return select;
    }

    public String getSqlExceptSelect(ConcurrentHashMap<String, String> searchInfo){
        String sqlExceptSelect = " from sys_permission p where 1=1 ";

        for(Entry<String, String> item : searchInfo.entrySet()){
            sqlExceptSelect += buildQuery(item);
        }
        sqlExceptSelect += " order by level asc, sort asc ";

        return sqlExceptSelect;
    }

    private String buildQuery(Entry<String, String> item){
        String sql = "";
        if(item.getKey().equals("pmsName")){
            sql += " and p.name like '%" + item.getValue() + "%' ";
        } else if(item.getKey().equals("pmsParentID")){
            sql += " and p.parent_id = " + item.getValue();
        } else if(item.getKey().equals("pmsLevel") && !"0".equals(item.getValue())){
            sql += " and p.level = " + item.getValue();
        } else if(item.getKey().equals("pmsType") && !"0".equals(item.getValue())){
            sql += " and p.type = " + item.getValue();
        } else if(item.getKey().equals("pmsID")){
            sql += " and p.id = " + item.getValue();
        } else if(item.getKey().equals("pmsUrl")){
            sql += " and p.url = '" + item.getValue() + "' ";
        } else if(item.getKey().equals("pmsDeleted") && !"-1".equals(item.getValue())){
            sql += " and p.is_deleted = " + item.getValue() + " ";
        }
        return sql;
    }
}
